package com.bds.snatch.system.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bds.snatch.model.TemplateUrl;
import com.bds.snatch.system.service.BalanceService;
import com.bds.snatch.system.service.MessageService;
import com.bds.snatch.system.service.ParseService;
import com.bds.snatch.system.util.SpringContextHolder;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;


public class ParseHtmlTask extends Thread {
    private static Logger logger = Logger.getLogger(PortalTask.class);
    private List<TemplateUrl> urls;
    private TemplateUrl info=null;

    public ParseHtmlTask(List<TemplateUrl> urls) {
        this.urls = urls;
    }
    public ParseHtmlTask(TemplateUrl info) {
        this.info = info;
    }
    @Override
    public void run() {
        System.out.println("--------------------解析任务开始执行-----------------------");
            NutDao apiDao = SpringContextHolder.getBean("dao");
            ParseService parseService = SpringContextHolder.getBean("parseService");
            MessageService messageService = SpringContextHolder.getBean("messageService");
            BalanceService balanceService = SpringContextHolder.getBean("balanceService");
            if(!CollectionUtils.isEmpty(urls)) {
                for (TemplateUrl info : urls) {
                    try {
                        String[] htmls = info.getHtml().split("##@##");
                        int size = htmls.length;
                        if(size>0) {
                            for (int i=0;i<size;i++) {
                                String html=htmls[i];
                                if(!StringUtils.isEmpty(html)) {
                                    Document doc = Jsoup.parse(html);
                                    apiDao.update(TemplateUrl.class, Chain.make("status", 2), Cnd.where("id", "=", info.getId()));
                                    balanceService.subtractBalance(info.getUser_id(), 1d);
                                    JSONObject j = new JSONObject();
                                    j.put("id", info.getId());
                                    j.put("status", "处理成功");
                                    messageService.addMessage(info.getUser_id(), info.getId(), "t_template_url", 5l,
                                            j.toJSONString());
                                    parseService.parseHtml(info.getId(), doc, null, true);
                                }
                            }
                        }
                    } catch (Exception ex) {
                        apiDao.update(TemplateUrl.class, Chain.make("status", 6), Cnd.where("id", "=", info.getId()));
                        continue;
                    }
                }
            }
            if(info!=null){
                try {
                    String[] htmls = info.getHtml().split("##@##");
                    int size = htmls.length;
                    if(size>0) {
                        for (int i=0;i<size;i++) {
                            String html=htmls[i];
                            if(!StringUtils.isEmpty(html)) {
                                Document doc = Jsoup.parse(html);
                                apiDao.update(TemplateUrl.class, Chain.make("status", 2), Cnd.where("id", "=", info.getId()));
                                balanceService.subtractBalance(info.getUser_id(), 1d);
                                JSONObject j = new JSONObject();
                                j.put("id", info.getId());
                                j.put("status", "处理成功");
                                messageService.addMessage(info.getUser_id(), info.getId(), "t_template_url", 5l,
                                        j.toJSONString());
                                parseService.parseHtml(info.getId(), doc, null, true);
                            }
                        }
                    }
                } catch (Exception ex) {
                    apiDao.update(TemplateUrl.class, Chain.make("status", 6), Cnd.where("id", "=", info.getId()));
                }
            }
    }
}
